package regpang.listviewtest.SwipeRecyclerView;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * 侧滑列表
 */
public class SwipeRecyclerActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("侧滑删除列表");
        final SwipeRecyclerView swipeRecyclerView = new SwipeRecyclerView(this);
        swipeRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        final List<String> strings = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            strings.add("---" + i);
        }
        final SwieRecyclerAdapter adapter = new SwieRecyclerAdapter(this, strings);
        swipeRecyclerView.setAdapter(adapter);
        /**
         * 既然是动画，就会有时间，我们把动画执行时间变大一点来看一看效果
         * 设置删除动画效果
         */
        DefaultItemAnimator defaultItemAnimator = new DefaultItemAnimator();
        defaultItemAnimator.setAddDuration(1000);
        defaultItemAnimator.setRemoveDuration(1000);
        swipeRecyclerView.setItemAnimator(defaultItemAnimator);

        swipeRecyclerView.setRightClickListener(new SwipeRecyclerView.OnRightClickListener() {
            @Override
            public void onRightClick(int position, String id) {
                Toast.makeText(SwipeRecyclerActivity.this, "侧滑点击删除", Toast.LENGTH_SHORT).show();
//                swipeRecyclerView.removeViewAt(position);
                adapter.notifyItemRemoved(position);
                strings.remove(position);
            }
        });
        setContentView(swipeRecyclerView);
    }
}
